package com.runjian.demo.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.runjian.demo.base.BusinessException;
import com.runjian.demo.base.Result;
import com.runjian.demo.demo.entity.SysConfig;
import com.runjian.demo.demo.entity.SysLog;
import com.runjian.demo.demo.model.SysConfigParamVO;
import com.runjian.demo.demo.model.SysConfigSaveVO;
import com.runjian.demo.demo.model.SysConfigVO;
import com.runjian.demo.demo.service.ISysConfigService;
import com.runjian.demo.demo.service.ISysLogService;
import com.runjian.demo.util.CommonUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Tag(name = "配置管理-接口")
@RestController
@RequestMapping("/api/config")
public class ConfigController {
    @Autowired
    private ISysLogService sysLogService;
    @Autowired
    private ISysConfigService sysConfigService;

    @Operation(summary = "配置分页")
    @PostMapping("/configPage")
    public Result<List<SysConfigVO>> configPage(@RequestBody SysConfigParamVO req) {
        IPage page = sysConfigService.page(new Page<>(req.getPageNum(), req.getPageSize()), new LambdaQueryWrapper<SysConfig>()
                .eq(StringUtils.isNotBlank(req.getCode()), SysConfig::getCode, req.getCode()));
        if (CollUtil.isEmpty(page.getRecords())) {
            return Result.page();
        }
        return Result.page(page, SysConfigVO.class);
    }


    @Operation(summary = "新增配置")
    @PostMapping("/addConfig")
    public Result<String> addConfig(@RequestBody SysConfigSaveVO req) {
        SysConfig sysConfig = BeanUtil.copyProperties(req, SysConfig.class);
        boolean res = sysConfigService.save(sysConfig);
        return Result.success(sysConfig.getId());
    }


    @Operation(summary = "修改配置")
    @PostMapping("/updateConfig")
    public Result<String> updateConfig(@RequestBody SysConfigSaveVO req) {
        SysConfig sysConfig = BeanUtil.copyProperties(req, SysConfig.class);
        if (StringUtils.isBlank(sysConfig.getId())) {
            return Result.error("ID必填");
        }
        boolean res = sysConfigService.updateById(sysConfig);
        return Result.success(sysConfig.getId());
    }

    @Operation(summary = "删除配置")
    @PostMapping("/deleteConfig")
    public Result<Boolean> deleteConfig(@RequestParam("ids") String ids) {
        List<String> strList = CommonUtil.toStrList(ids);
        if (CollUtil.isEmpty(strList)) {
            throw new BusinessException("ids必填");
        }
        boolean res = sysConfigService.remove(new LambdaQueryWrapper<SysConfig>()
                .in(SysConfig::getId, strList));
        return Result.success(res);
    }

    @Operation(summary = "根据编码获取配置")
    @GetMapping("/configByCode")
    public Result<SysConfigVO> configByCode(@RequestParam(value = "code") String code) {
        SysConfig one = sysConfigService.getOne(new LambdaQueryWrapper<SysConfig>().eq(SysConfig::getCode, code));
        if (Objects.isNull(one)) {
            return Result.success(null);
        }
        SysConfigVO sysConfigVO = BeanUtil.copyProperties(one, SysConfigVO.class);
        return Result.success(sysConfigVO);
    }

    @Operation(summary = "根据Id获取日志列表")
    @GetMapping("/getById")
    public Result<List<SysLog>> getById(@RequestParam(value = "id") String id) {
        return Result.success(sysLogService.list(new LambdaQueryWrapper<SysLog>().eq(StringUtils.isNotBlank(id), SysLog::getId, id)));
    }

    @Operation(summary = "获取日志分页列表")
    @GetMapping("/logPage")
    public Result<List<SysLog>> logPage(@RequestParam(value = "pageNum", defaultValue = "0", required = false) Long pageNum,
                                        @RequestParam(value = "pageSize", defaultValue = "10", required = false) Long pageSize,
                                        @RequestParam(value = "id", required = false) String id) {
        IPage page = sysLogService.page(new Page<>(pageNum, pageSize), new LambdaQueryWrapper<SysLog>().eq(StringUtils.isNotBlank(id), SysLog::getId, id));
        return Result.page(page);
    }

}
